package com.uzai.console.service.pyq.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.pyq.pyqcontenttemplate.PyqContentTemplateDto;
import com.uzai.console.dto.pyq.pyqcontenttemplate.PyqContentTemplateIdDto;
import com.uzai.console.dto.pyq.pyqcontenttemplate.PyqContentTemplateQuery;
import com.uzai.console.entity.PyqContentTemplate;
import com.uzai.console.mapper.PyqContentTemplateMapper;
import com.uzai.console.service.pyq.PyqContentTemplateService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.pyq.pyqcontenttemplate.PyqContentTemplateVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 朋友圈内容模板
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class PyqContentTemplateServiceImpl implements PyqContentTemplateService {

    Logger logger = LoggerFactory.getLogger(PyqContentTemplateServiceImpl.class);

    @Autowired
    private PyqContentTemplateMapper pyqContentTemplateMapper;

    /**
     * 查询朋友圈内容模板列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqContentTemplateQuery
     * @return null
     */
    public Page<PyqContentTemplateVo> findPyqContentTemplateList(PyqContentTemplateQuery pyqContentTemplateQuery){
        //新建返回到页面的LIST对象
        List<PyqContentTemplateVo> pyqContentTemplatePageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<PyqContentTemplate> page = new Page<>();
        pyqContentTemplateQuery.convert(page);
        ArrayList<PyqContentTemplate> pyqContentTemplateList =  pyqContentTemplateMapper.findByList(page, pyqContentTemplateQuery);
        if(pyqContentTemplateList != null && pyqContentTemplateList.size() > 0){
            for (PyqContentTemplate pyqContentTemplate : pyqContentTemplateList){
                PyqContentTemplateVo pyqContentTemplatePageVo = new PyqContentTemplateVo();
                BeanUtils.copyProperties(pyqContentTemplate, pyqContentTemplatePageVo);
                pyqContentTemplatePageVoList.add(pyqContentTemplatePageVo);
            }
        }

        //返回到页面分页对象
        Page<PyqContentTemplateVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(pyqContentTemplatePageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询朋友圈内容模板详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqContentTemplateIdDto
     * @return null
     */
    public PyqContentTemplateVo findPyqContentTemplate(PyqContentTemplateIdDto pyqContentTemplateIdDto){
        if(pyqContentTemplateIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        PyqContentTemplate pyqContentTemplate = pyqContentTemplateMapper.selectById(pyqContentTemplateIdDto.getId(), pyqContentTemplateIdDto.getMerId());

        if(pyqContentTemplate == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该朋友圈内容模板不存在");
        }

        PyqContentTemplateVo pyqContentTemplateVo = new PyqContentTemplateVo();
        BeanUtils.copyProperties(pyqContentTemplate, pyqContentTemplateVo);

        return pyqContentTemplateVo;
    }

    /**
     * 添加朋友圈内容模板
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqContentTemplateDto
     * @return null
     */
    public void addPyqContentTemplate(PyqContentTemplateDto pyqContentTemplateDto){

        if(pyqContentTemplateDto.getPlatform() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择平台");
        }

        if(StringUtils.isBlank(pyqContentTemplateDto.getName())){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入模板名称");
        }

        //验证内容内容
        String contentStr = pyqContentTemplateDto.getContent();
        if(StringUtils.isBlank(contentStr)){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请添加内容");
        }

        Integer now = DateUtil.getNowTime();
        PyqContentTemplate pyqContentTemplate = new PyqContentTemplate();
        pyqContentTemplate.setId(IdWorker.getId());
        pyqContentTemplate.setMerId(pyqContentTemplateDto.getMerId());
        pyqContentTemplate.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());
        pyqContentTemplate.setPlatform(pyqContentTemplateDto.getPlatform());
        pyqContentTemplate.setName(pyqContentTemplateDto.getName());
        pyqContentTemplate.setContent(pyqContentTemplateDto.getContent());
        pyqContentTemplate.setCreateTime(now);
        pyqContentTemplate.setUpdateTime(now);
        pyqContentTemplateMapper.insert(pyqContentTemplate);
    }

    /**
     * 修改朋友圈内容模板
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqContentTemplateDto
     * @return null
     */
    public void updatePyqContentTemplate(PyqContentTemplateDto pyqContentTemplateDto){
        if(pyqContentTemplateDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        if(StringUtils.isBlank(pyqContentTemplateDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入模板名称");
        }

        //验证内容内容
        String contentStr = pyqContentTemplateDto.getContent();
        if(StringUtils.isBlank(contentStr)){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请添加内容内容");
        }

        PyqContentTemplate pyqContentTemplate = pyqContentTemplateMapper.selectById(pyqContentTemplateDto.getId(), pyqContentTemplateDto.getMerId());
        if(pyqContentTemplate == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该内容模板不存在");
        }

        pyqContentTemplate.setName(pyqContentTemplateDto.getName());
        pyqContentTemplate.setContent(pyqContentTemplateDto.getContent());
        Integer now = DateUtil.getNowTime();
        pyqContentTemplate.setUpdateTime(now);
        pyqContentTemplateMapper.updateById(pyqContentTemplate);
    }

    /**
     * 删除
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqContentTemplateIdDto
     * @return null
     */
    public void deletePyqContentTemplate(PyqContentTemplateIdDto pyqContentTemplateIdDto){
        if(pyqContentTemplateIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        // TODO: 2022/3/8  查询该内容模板是否已经给朋友圈占用了

        pyqContentTemplateMapper.deleteById(pyqContentTemplateIdDto.getId(), pyqContentTemplateIdDto.getMerId());
    }

    /**
     * 查询朋友圈内容模板下拉列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param query
     * @return null
     */
    public List<PyqContentTemplateVo> findSelectList(PyqContentTemplateQuery query){
        List<PyqContentTemplateVo> pyqContentTemplateVoList = new ArrayList<>();
        List<PyqContentTemplate> pyqContentTemplateList =  pyqContentTemplateMapper.findByList(query);
        if(pyqContentTemplateList != null && pyqContentTemplateList.size() > 0){
            for (PyqContentTemplate pyqContentTemplate : pyqContentTemplateList){
                PyqContentTemplateVo pyqContentTemplateVo = new PyqContentTemplateVo();
                BeanUtils.copyProperties(pyqContentTemplate, pyqContentTemplateVo);
                pyqContentTemplateVoList.add(pyqContentTemplateVo);
            }
        }
        return pyqContentTemplateVoList;
    }
}
